#ifndef __TIMER_H__
#define __TIMER_H__

#include "Spark/Net/CallbackDefs.h"
#include "Spark/Utils/TimePoint.h"
#include <atomic>
#include <memory>
#include <chrono>

/**
 * @brief A wrapper clas that wraps a user-defined functor with a TimePoint,
 * which determines when this functor is about to run
 */
class Timer
{
  public:
    Timer(Task task, TimePoint when, SysTime::duration interval);
    ~Timer();
    void run();
    void restart(TimePoint when);
    bool operator<(const Timer& that) const
    {
        return when_ < that.when_;
    }
    bool operator>(const Timer& that) const
    {
        return when_ > that.when_;
    }
    bool isRepeat() const
    {
        return repeat_;
    }
    const TimePoint when() const
    {
        return when_;
    }
    const TimerID id() const
    {
        return id_;
    }

  private:
    static std::atomic<TimerID> timersCreated;
    Task task_;
    TimePoint when_;
    const SysTime::duration interval_;
    const bool repeat_{false};
    const TimerID id_;
};

#endif  // __TIMER_H__